import request from '@/config/axios'

// 能耗设备 API 路径
export const deviceApi = {
  page: '/energy/device/page',
  create: '/energy/device/create',
  update: '/energy/device/update',
  delete: '/energy/device/delete',
  get: '/energy/device/get',
  list: '/energy/device/list',
  export: '/energy/device/export'
}

// 能耗设备类型
export interface DeviceVO {
  id: number
  deviceId: string
  deviceName: string
  model: string
  location: string
  energyType: string
  voltage: number
  currents: number
  activePower: number
  energyConsumption: number
  temperature: number
  status: string
  createTime: Date
}

// 能耗设备分页请求参数
export interface DevicePageReqVO extends PageParam {
  deviceId?: string
  deviceName?: string
  model?: string
  location?: string
  energyType?: string
  status?: string
  createTime?: Date[]
}

// 获取能耗设备分页列表
export const getDevicePage = async (params: DevicePageReqVO) => {
  return await request.get<PageResult<DeviceVO>>({ url: deviceApi.page, params })
}

// 获取能耗设备详情
export const getDevice = async (id: number) => {
  return await request.get<DeviceVO>({ url: deviceApi.get, params: { id } })
}

// 新增能耗设备
export const createDevice = async (data: Omit<DeviceVO, 'id' | 'createTime'>) => {
  return await request.post<number>({ url: deviceApi.create, data })
}

// 修改能耗设备
export const updateDevice = async (data: Omit<DeviceVO, 'createTime'>) => {
  return await request.put<boolean>({ url: deviceApi.update, data })
}

// 删除能耗设备
export const deleteDevice = async (id: number) => {
  return await request.delete<boolean>({ url: deviceApi.delete, params: { id } })
}

// 能耗类型选项
export const energyTypeOptions = [
  { label: '电力', value: 'electricity' },
  { label: '水', value: 'water' },
  { label: '天然气', value: 'gas' },
  { label: '热力', value: 'heat' },
  { label: '压缩空气', value: 'compressed_air' },
  { label: '蒸汽', value: 'steam' }
]

// 设备状态选项
export const deviceStatusOptions = [
  { label: '正常', value: 'normal', type: 'success' },
  { label: '离线', value: 'offline', type: 'info' },
  { label: '故障', value: 'fault', type: 'danger' },
  { label: '维护中', value: 'maintenance', type: 'warning' }
]

// 获取所有能耗设备列表
export const getDeviceList = async () => {
  return await request.get<DeviceVO[]>({ url: deviceApi.list })
}

// 导出能耗设备数据
export const exportDevice = async (params: DevicePageReqVO) => {
  try {
    const filename = '能耗设备数据_' + new Date().getTime() + '.xlsx'
    console.log('开始下载请求:', deviceApi.export, params)
    const res = await request.download({
      url: deviceApi.export,
      params
    })
    console.log('下载请求完成，数据大小:', res ? res.size : '未知')

    if (!res || res.size === 0) {
      console.error('下载内容为空')
      throw new Error('下载内容为空')
    }

    // 检查响应类型
    console.log('响应类型:', res.type)

    // 创建Blob对象，并执行下载
    const blob = new Blob([res], {
      type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    })
    const url = window.URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.href = url
    link.setAttribute('download', filename)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link) // 移除a标签
    window.URL.revokeObjectURL(url) // 释放url
    return res
  } catch (error) {
    console.error('导出处理过程中出错:', error)
    throw error
  }
}
